﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCOA.Helper;

namespace MVCOA
{
    public class EsDetailController : Controller
    {
        public ActionResult Show()
        {
            IBLL.IEsDetailBLL EsDetail = OperateContext.Current.BLLSession.IEsDetailBLL;
            var EsDetailList = EsDetail.GetListBy(g => g.isDel == false);
            return View(EsDetailList);
        }
        public ActionResult List(string name)  
        {
            IBLL.ICate_FisrtBLL fBll = OperateContext.Current.BLLSession.ICate_FisrtBLL;
            fBll.GetListBy(a => true);
            
            IBLL.IUrlNameBLL fbb = OperateContext.Current.BLLSession.IUrlNameBLL;
           
            return View();   
        }

        public ActionResult Items(FormCollection form)
        {
            int id = int.Parse(form["type"]);
            int parentId = int.Parse(form["parentId"]);
            if (id == 2)
            {
                IBLL.ICate_SecondBLL secBll = OperateContext.Current.BLLSession.ICate_SecondBLL;
                List<MODEL.Cate_Second> secList = secBll.GetListBy(q => q.Cate_Fisrt.id == parentId).Select(a => a.ToPOCO2()).ToList();
                MODEL.JsonModel model = new MODEL.JsonModel()
                {
                    Data = secList,
                    Statu = "ok",
                    Msg = "这是二级分类"
                };
                return Json(model, JsonRequestBehavior.AllowGet);
            }
            else if (id == 3)
            {
                IBLL.ICate_ThirdBLL thiBll = OperateContext.Current.BLLSession.ICate_ThirdBLL;
                List<MODEL.Cate_Third> thiList = thiBll.GetListBy(q => q.Cate_Second.id == parentId).Select(a => a.ToPOCO2()).ToList();
                MODEL.JsonModel model = new MODEL.JsonModel()
                {
                    Data = thiList,
                    Statu = "ok",
                    Msg = "这是三级分类"
                };
                return Json(model, JsonRequestBehavior.AllowGet);
            }
            else {
                     MODEL.JsonModel model = new MODEL.JsonModel()
                {
                    Statu="err"
                };
                     return Json(model,JsonRequestBehavior.AllowGet);
            }
          

           
        }

        [HttpPost]
        public RedirectToRouteResult Add(MODEL.EsDetail model,FormCollection form) {
            IBLL.IEsDetailBLL EsDetailOp = OperateContext.Current.BLLSession.IEsDetailBLL;
            model.date = DateTime.Now;
            model.cateSecondId = form["secondIdItems"];
            model.cateThirdId = form["thirdIdItems"];
            EsDetailOp.Add(model);       
            return RedirectToAction("Show", "EsDetail");
        }
        [HttpGet]
        public ActionResult Add()
        {
            IBLL.ICate_FisrtBLL CateFirst = OperateContext.Current.BLLSession.ICate_FisrtBLL;
            IBLL.ICate_SecondBLL CateSecond = OperateContext.Current.BLLSession.ICate_SecondBLL;
            IBLL.ICate_ThirdBLL CateThird = OperateContext.Current.BLLSession.ICate_ThirdBLL;

            List<SelectListItem> firstIdItems = new List<SelectListItem>();
            firstIdItems.Add(new SelectListItem { Text = "-请选择-",Value="" ,Selected=true });
            foreach (var cate in CateFirst.GetListBy(q => true))
            {
                firstIdItems.Add(new SelectListItem { Text = cate.name, Value = cate.id + "" });
            }
       

            List<SelectListItem> oldNewItems = new List<SelectListItem>() { 
                new SelectListItem { Text = "全新" ,Value ="0",Selected = true},
                new SelectListItem { Text = "95成" ,Value ="1",Selected=false},
                new SelectListItem { Text = "9成" ,Value ="2",Selected = false },
                new SelectListItem { Text = "8成" ,Value ="3" ,Selected = false},
                new SelectListItem { Text = "7成及以下" ,Value ="4",Selected = false },

            };
            ViewBag.firstIdItems = firstIdItems;
            ViewBag.xinjiu = oldNewItems;
            return View();
        }

    }
}
